/*
* DataGird  0.1 (beta) - MXTags ( Original idea from Adobe Flex )
* Copyright (c) 2010, Rafael Lúcio, poste9@gmail.com
* Dual licensed under the MIT and GPL licenses
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
* Date:2010-09-13
* Modules: mxtag.combobox.js, mxtag.listbox.js, mxtag.datagrid.js, mxtag.collection.js
*/

/*Faz o IE interpretar as tags novas*/
if ($.browser.msie) {document.createElement("DataGrid");document.createElement("ComboBox");
                     document.createElement("ListBox");document.createElement("DataGridColumn");}
(function($){
    $.fn.extend({
        mxtags:function(options) {
            this.scripts = new Array();
            // Define o lugar onde está a biblioteca
            this.mxtagPath = $("head script[src*=mxtag]")[0].getAttribute("src").replace("jquery.mxtag.js","modules/mxtag.%s.js");
            
            this.toCamel = function(string){
                return string.replace(/^([a-z])(.*)/, function($1,$2,$3){return $2.toUpperCase() + $3.toLowerCase();});
            };
            this.execute = function() {

                window.MXTAG_PATH = this.mxtagPath;
                this.mxTagsAvaible = {datagrid: window['DataGrid'],listbox: window['ListBox'],combobox: window['ComboBox'],datagridcolumn : window['DataGridColumn']};
                
                for(var i = 0; i < this.length; i++) {
                    var element = this[i];
                    // Cria uma nova instancia do componente carregado
                    var mxtag = new this.mxTagsAvaible[element.tagName.toLowerCase()];
                    // Seta o elemento que está no html
                        mxtag.setElement($(element));
                    // Seta os atributos da tag
                    for(var j = 0; j < element.attributes.length; j++){
                        try {
                            mxtag["set"+this.toCamel(element.attributes[j].nodeName)](element.attributes[j].nodeValue);
                        } catch(ex) {
                            if (window.console)
                            console.error("set"+this.toCamel(element.attributes[j].nodeName) + " is not a function");
                        }
                    }
                    mxtag.makeComponentData();
                }
            }
            this.execute();
        }
    });
    
})(jQuery);